<?php 

class HtmlController extends Controller{
	public function init(){
		if(!isset($_SESSION['aid']) || !isset($_SESSION['adminname'])){
			go(U('Admin/Login/index'));
		}
	}
	public function createHtml($CA,$path){
//		开启缓冲区
		ob_start();
//		访问页面
		A('Index/' . $CA);
//		获取缓冲区内容
		$data = ob_get_contents();
//		清除缓冲区
		ob_clean();
//		获取文件目录
		$dir = dirname($path);
//		如果目录不存在就创建目录
		is_dir($dir) || mkdir($dir,0777,true);
//		将数据写入目录下文件
		file_put_contents($path, $data);
	}
	public function index(){
	   $this->createHtml('Index/index','./index.html');
		$this->success('生成成功',U('Admin/Index/welcome'));
	}
	public function _list(){
	    //获得生成静态的数据
		$cateData = K('Category')->where("is_listhtml=1")->all();
		
		//循环生成
		foreach ($cateData as $v) {
			$_GET['cid'] = $v['cid'];
			//指定分页静态地址
			Page::$staticUrl = __ROOT__ . '/Static/' . C('INDEX_TPL_STYLE') . '/' . $v['htmldir'] . '/{page}.html';
			$this->createHtml('List/index', 'Static/' . C('INDEX_TPL_STYLE') . '/' . $v['htmldir'] . '/index.html');
			//生成分页静态页面
			for ($i=1; $i <= Page::$staticTotalPage; $i++) {
				$_GET['page'] = $i; 
				$this->createHtml('List/index', 'Static/' . C('INDEX_TPL_STYLE') . '/' . $v['htmldir'] . '/' . $i . '.html');
			}
			
		}
		$this->success('生成成功',U('Admin/Index/welcome'));
	}
	public function content(){
		$arcData = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("is_archtml=1")->all();
		foreach ($arcData as $v) {
			$_GET['aid'] = $v['aid'];
			$this->createHtml('Content/index', 'Static/' . C('INDEX_TPL_STYLE') . '/' . $v['htmldir'] . '/arc/' . $v['aid'] . '.html');
		}
		$this->success('生成成功',U('Admin/Index/welcome'));
	}
}
